<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-04-21 15:27:09
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-26 21:52:44
 */
namespace app\api\controller;
use hg\apidoc\annotation as Apidoc;
use app\admin\model\Banner as BannerModel;; 

/**
 * @Apidoc\Title("轮播图")
 */
class Banner extends ApiBase
{

    /**
     * @Apidoc\Title("获取轮播图列表")
     * @Apidoc\Url("/api/get_banner")
     * @Apidoc\Method("get")
     * @Apidoc\Param("position", type="int",require=true, desc="图片位置 1 首页")
     * @Apidoc\Returned("get_one", type="int", default=0,desc="是否只取一张, 用于页面某位置的单张banner")
     * @Apidoc\Returned("image", type="string", desc="图片地址")
     * @Apidoc\Returned("url", type="string", desc="跳转地址")
     */
    public function get_banner() {
        try {
            $position = param_check('position', false, '缺少图片位置');
            $get_one  = param_check('get_one', 1); // 是否只取一张, 用于页面某位置的单张banner
            $where    = [
                'status'   => 1,
                'position' => $position
            ];
            $hook = BannerModel::where($where)
                ->order('sort desc, id DESC')
                ->field('image, type, url');
            $info = $get_one == 1 ? $hook->find() : $hook->select();
            json_response(200, 'success', $info??[]);
        } catch (\Exception $e) {
            json_response(400, '接口错误', [
                'info' => $e->getMessage(),
                'line' => $e->getLine()
            ]);
        }
    }
}